/**
 * Copyright (C) 2017-2099 All right reserved
 * For free
 *
 * @file timer.h
 * @brief
 * @author qigaohua, qigaohua168@163.com
 * @version 1.0.1
 * @date 2021-04-20
 *
 */



#ifndef __TIMER_H__
#define __TIMER_H__

#define MULTI_PTHREAD  // 多线程环境

#ifdef MULTI_PTHREAD
#include <pthread.h>
#endif


#define MAX_INT         0x0fffffff
#define PARENT(i)       ((i)/(2))
#define LEFTCHILD(i)    ((i)*(2))
#define RIGHTCHILD(i)   (((i)*(2))+(1))



typedef int (*ProcessFunc)(void *);

typedef struct TimerNode_ {
    unsigned long time_ms;
    ProcessFunc process_node_func;
    void *data;
} TimerNode_t ;


typedef struct Timer_ {
    TimerNode_t   *node_list ;
    int            headindex ;
    int            lastindex ;
    unsigned int   max_num   ;

    ProcessFunc ProcessTimerNodeData;

#ifdef MULTI_PTHREAD
    pthread_mutex_t mutex;
    pthread_cond_t cond;
#endif
} Timer_t;

enum Timer_cmd {
    CMD_OFF = 0,
    CMD_EXIT,
    CMD_HAVE_DATA,
    CMD_ADD_NODE,
};



Timer_t *Timer_Init(unsigned int node_num, ProcessFunc func);
void Timer_Destroy(Timer_t *T);
int  Timer_AddNode(Timer_t *T, void *data, unsigned long time_ms, ProcessFunc func);
int  Timer_DelNode(Timer_t *T, void **data, unsigned long *time_ms);
int  Timer_IsEmpty(Timer_t *T);
int  Timer_IsFull(Timer_t *T);
// int MinHeapGetMinNode(Timer_t *T, void **data, unsigned long *time_ms);
int Timer_GetMinNode(Timer_t *T, void **data, unsigned long *time_ms, ProcessFunc *func);
int Timer_IsWait();
int Timer_Tell(enum Timer_cmd cmd);
int Timer_Loop(Timer_t *T);




#endif
